package com.topisv.tms.task;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.topisv.tms.entity.task.MdSendOutTask;
import com.topisv.tms.service.md.MdSendOutTaskService;
import com.topisv.tms.utils.InitProperties;

@Component
@Lazy(false)
public class MdSendOutProvider implements InitializingBean {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private BlockingQueue<MdSendOutTask> tasks = new ArrayBlockingQueue<MdSendOutTask>(1000);
	@Autowired
	private MdSendOutTaskService mdSendOutTaskService;

	@Scheduled(cron = "0 0/1 * * * *")
	public void executor() {
		try {
			mdSendOutTaskService.lockData(100);
			List<MdSendOutTask> data = mdSendOutTaskService.findLockedTask();
			for (MdSendOutTask mdSendOutTask : data) {
				tasks.put(mdSendOutTask);
			}
		} catch (Exception e) {
			logger.error("MdSendOutProvider case some error.", e);
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int i = 0; i < Integer.valueOf(InitProperties.get("mdwokrs.num").toString()); i++) {
			new MdSendOutTaskWorker(tasks, mdSendOutTaskService).start();
		}
	}
}
